import unittest
import time
from merge_sort import merge_sort
from merge_sort_generator import generate_random_array

class TestMergeSort(unittest.TestCase):
    def test_sorted_array(self):
        """测试排序功能是否正确"""
        arr = [5, 2, 9, 1, 5, 6]
        expected = sorted(arr)
        print(f"Before sorting: {arr}")
        start_time = time.time()
        merge_sort(arr)
        end_time = time.time()
        print(f"After sorting: {arr}")
        print(f"Test runtime: {end_time - start_time:.6f} seconds")
        self.assertEqual(arr, expected)
        print("Test case passed: test_sorted_array\n")

    def test_empty_array(self):
        """测试空数组"""
        arr = []
        expected = []
        print(f"Before sorting: {arr}")
        start_time = time.time()
        merge_sort(arr)
        end_time = time.time()
        print(f"After sorting: {arr}")
        print(f"Test runtime: {end_time - start_time:.6f} seconds")
        self.assertEqual(arr, expected)
        print("Test case passed: test_empty_array\n")

    def test_single_element_array(self):
        """测试单元素数组"""
        arr = [42]
        expected = [42]
        print(f"Before sorting: {arr}")
        start_time = time.time()
        merge_sort(arr)
        end_time = time.time()
        print(f"After sorting: {arr}")
        print(f"Test runtime: {end_time - start_time:.6f} seconds")
        self.assertEqual(arr, expected)
        print("Test case passed: test_single_element_array\n")

    def test_large_random_array(self):
        """测试随机生成的大数组"""
        arr = generate_random_array(1000, 1, 10000)
        expected = sorted(arr)
        print(f"Before sorting: {arr[:10]}... (truncated)")
        start_time = time.time()
        merge_sort(arr)
        end_time = time.time()
        print(f"After sorting: {arr[:10]}... (truncated)")
        print(f"Test runtime: {end_time - start_time:.6f} seconds")
        self.assertEqual(arr, expected)
        print("Test case passed: test_large_random_array\n")

if __name__ == "__main__":
    # 测试运行时间
    start_time = time.time()
    unittest.main(exit=False)
    end_time = time.time()
    print(f"Total runtime: {end_time - start_time:.6f} seconds")